﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		SparkViewEngine.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using Dexter.Services;
using Spark;
using Spark.FileSystem;
using Spark.Web.Mvc;

namespace Dexter.Web.Mvc.ViewEngines {
	public class SparkViewEngine : IViewEngine {
		static readonly string[] themes;
		readonly IConfigurationService configurationService;
		readonly IViewEngine defaultEngine;
		readonly Dictionary <string , IViewEngine> themedEngines = new Dictionary <string , IViewEngine> ( );

		static SparkViewEngine ( ) {
			themes = new DirectoryInfo ( AppDomain.CurrentDomain.BaseDirectory.Append ( "Themes\\" ) ).GetDirectories ( ).Select ( x => x.Name ).ToArray ( );
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="SparkViewEngine"/> class.
		/// </summary>
		/// <param name="configurationService">The configuration service.</param>
		public SparkViewEngine ( IConfigurationService configurationService ) {
			this.configurationService = configurationService;
			ISparkServiceContainer container = SparkEngineStarter.CreateContainer ( );
			defaultEngine = container.GetService <IViewEngine> ( );
			container.GetService <IViewFolder> ( );
		}

		public IViewEngine[] ViewsEngine {
			get { return themedEngines.Select ( x => x.Value ).ToArray ( ); }
		}

		#region IViewEngine Members

		/// <summary>
		/// Finds the specified partial view by using the specified controller context.
		/// </summary>
		/// <param name="controllerContext">The controller context.</param>
		/// <param name="partialViewName">The name of the partial view.</param>
		/// <param name="useCache">true to specify that the view engine returns the cached view, if a cached view exists; otherwise, false.</param>
		/// <returns>The partial view.</returns>
		ViewEngineResult IViewEngine.FindPartialView ( ControllerContext controllerContext , string partialViewName , bool useCache ) {
			IViewEngine viewEngine = GetCorrectViewEngine ( );
			ViewEngineResult result = viewEngine.FindPartialView ( controllerContext , partialViewName , useCache );

			return result;
		}

		/// <summary>
		/// Finds the specified view by using the specified controller context.
		/// </summary>
		/// <param name="controllerContext">The controller context.</param>
		/// <param name="viewName">The name of the view.</param>
		/// <param name="masterName">The name of the master.</param>
		/// <param name="useCache">true to specify that the view engine returns the cached view, if a cached view exists; otherwise, false.</param>
		/// <returns>The page view.</returns>
		ViewEngineResult IViewEngine.FindView ( ControllerContext controllerContext , string viewName , string masterName , bool useCache ) {
			IViewEngine viewEngine = GetCorrectViewEngine ( );

			ViewEngineResult result = viewEngine.FindView ( controllerContext , viewName , masterName , useCache );

			return result;
		}

		/// <summary>
		/// Releases the specified view by using the specified controller context.
		/// </summary>
		/// <param name="controllerContext">The controller context.</param>
		/// <param name="view">The view.</param>
		void IViewEngine.ReleaseView ( ControllerContext controllerContext , IView view ) {
			IViewEngine viewEngine = GetCorrectViewEngine ( );
			viewEngine.ReleaseView ( controllerContext , view );
		}

		#endregion

		public void AutoRegisterThemes ( ) {
			themes.ForEach ( this.AddTheme );
		}

		void AddTheme ( string name ) {
			IViewFolder themeFolder = new VirtualPathProviderViewFolder ( "~/Themes/" + name + "/Views" );
			ISparkServiceContainer container = SparkEngineStarter.CreateContainer ( );
			container.SetService ( themeFolder );
			themedEngines.Add ( name , container.GetService <IViewEngine> ( ) );
		}

		IViewEngine GetCorrectViewEngine ( ) {
			IViewEngine engine;

			if ( themedEngines.TryGetValue ( configurationService.Configuration.ThemeName , out engine ) ) {
				return engine;
			}

			return defaultEngine;
		}
	}
}
